# -*- coding: UTF-8 -*-
import direct.actor.Actor as ACTOR
import  game.elements.role.role as GROLE
import panda3d.core as CORE
import game.elements.props.weapon as GWEAPON
import game.elements.role.role_data as GRDATA
class Player(GROLE.GameRole):
    
    def initPlayer(self):
        self.ralph = ACTOR.Actor("resource/models/kenxi/eve", #Load our animated charachter
                           {"run":"resource/models/kenxi/eve_run",
                            'walk' : "resource/models/kenxi/eve_walk"})
        self.ralph.reparentTo(render)
        self.ralph.setScale(.2)
        self.ralph.setPos(self.startPos)
        self.ralph.setHpr(0,0,0)
        self.__initRoleCollision__()
        self.rightHand = self.ralph.exposeJoint(None, 'modelRoot', 'RightHand')
        sword = GWEAPON.Weapon();
        sword.initWeapon(self.rightHand)
        ################
        self.isJump = False
        self.jumperTimer = 0 
        self.start_z = 0
        
        ################
        self.isAttack = False
        self.atkTimer = 0 
        self.start_atk1 = 0
        self.start_atk2 = 0
        
        role_data = {"id":"r00001","name":"jack","hp":100,"mp":200,"atk":10,"dfd":5,"spd":300,"sight_range":6,
            "xyz":(0,0,0),"hpr":(0,0,0),
            "equip":{
               "weapon":{"eid":"100001","name":"sword","atk":10,"dfd":0,"spd":0,"atk_rng":2},
               "armor":{"eid":"200001","name":"cloth","atk":0,"dfd":5,"spd":0,"atk_rng":0},
               "boost":{"eid":"300001","name":"boost","atk":0,"dfd":0,"spd":50,"atk_rng":0}
              },"prop":["400001","400002","400003"]}
        self.player_data = GRDATA.RoleData(role_data)
    
    def attack(self):
        self.rightHand.setP(0)
        self.ralph.setH(self.start_atk2+self.atkTimer*(60-self.atkTimer))
    
    def jump(self):
        jump_hight = self.start_z+self.jumperTimer*(60-self.jumperTimer)*0.002
        self.ralph.setZ(jump_hight)
        self.ralph.loop("run")
           
    def __initRoleCollision__(self):
        self.ralphGroundRay = CORE.CollisionRay()
        self.ralphGroundRay.setOrigin(0,0,1000)
        self.ralphGroundRay.setDirection(0,0,-1)
        self.ralphGroundCol = CORE.CollisionNode('ralphRay')
        self.ralphGroundCol.addSolid(self.ralphGroundRay)
        self.ralphGroundCol.setFromCollideMask(CORE.BitMask32.bit(0))
        self.ralphGroundCol.setIntoCollideMask(CORE.BitMask32.allOff())
        self.ralphGroundColNp = self.ralph.attachNewNode(self.ralphGroundCol)
        self.ralphGroundHandler = CORE.CollisionHandlerQueue()
        self.game_obj.CTrav.addCollider(self.ralphGroundColNp, self.ralphGroundHandler)
    
    def move(self,start_pos,end_pos):
        pass
    def resetPlayer(self,startpos):
        entries = []
        #获得碰撞后的实体
        for i in range(self.ralphGroundHandler.getNumEntries()):
            entry = self.ralphGroundHandler.getEntry(i)
            entries.append(entry)
        entries.sort(lambda x,y: cmp(y.getSurfacePoint(render).getZ(),
                                     x.getSurfacePoint(render).getZ()))
        if (len(entries)>0) and (entries[0].getIntoNode().getName() == "terrain" ) :
            if self.isJump==False :
                self.ralph.setZ(entries[0].getSurfacePoint(render).getZ())
            else :
                if self.ralph.getZ()<= entries[0].getSurfacePoint(render).getZ():
                    self.isJump = False
        elif  self.isJump==True :
            #碰到东西了 直接掉到地面
            if entries[0].getIntoNode().getName() != "terrain" :
                for enty in entries:
                    if enty.getIntoNode().getName() == "terrain":
                        self.ralph.setPos(startpos)
                        self.ralph.setZ(enty.getSurfacePoint(render).getZ())
                        self.isJump = False
                        break;
                   
            
        else:  
            self.ralph.setPos(startpos)
  
    